<?php
/**
 * Created by PhpStorm.
 * User: czm
 * Date: 2019-07-17
 * Time: 16:04
 */
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Validate;

class Telephone extends Controller
{
    public function telephoneList()
    {
        $list = Db::name('picture')->order('id desc')->select();
//        $this->assign('list',$list);
//        $data = $list['phone'];
        return json_encode($list,JSON_FORCE_OBJECT);
    }

    public function telephoneAdd()
    {

        if (request()->isPost())
        {
            $data = input('post.');
//            var_dump($data);exit();
            $where = [
                'phone'=> $data['phone']
            ];
            $re = Db::name('telephone')->where($where)->find();
            if ($re){
                $dat = ['code' => 402, 'msg' => '手机号存在'];
                return json_encode($dat, TRUE);
            }
            $res = Db::name('telephone')->insert($data);
            if ($res)
            {
                $dat = ['code' => 200, 'msg' => '添加成功'];
                return json_encode($dat, TRUE);
            }else{
                $dat = ['code' => 403, 'msg' => '添加失败'];
                return json_encode($dat, TRUE);
            }

        }
        else {
            return json_encode(403);
        }
    }

    public function telephoneEdit()
    {
        $id = input('id');
        $re = Db::name('telephone')->where('id',$id)->find();
        if ($re){
            $da = ['code' => 200, 'msg' => '返回修改地址信息'];
            return json_encode($da,TRUE);
        }
        if (request()->isPost()){
            $dat = input('post.');
            $r = Db::name('telephone')->where('uid',$id)->update($dat);
            if ($r)
            {
                $dat = ['code' => 200, 'msg' => '修改成功'];
                return json_encode($dat);
            }else{
                $dat = ['code' => 403, 'msg' => '修改失败'];
                return json_encode($dat, TRUE);
            }
        }
    }

    public function addressDel()
    {
        $id = input('id');
        $r = Db::name('telephone')->where('id',$id)->delete($id);
        if ($r){
            $da = ['code' => 200, 'msg' => '删除成功'];
            return json_encode($da);
        }else{
            $da = ['code' => 403, 'msg' => '失败'];
            return json_encode($da,TRUE);

        }
    }
}